package com.daykw.douyin;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;

import com.daykw.douyin.task.VideoAuthSearchThread;
import com.daykw.douyin.task.VideoAuthThread;
import com.daykw.douyin.task.VideoAuthVideoThread;
import com.daykw.douyin.task.VideoCmtThread;
import com.daykw.douyin.task.VideoExtThread;
import com.daykw.douyin.task.VideoInfoThread;
import com.daykw.douyin.task.VideoLoginThread;
import com.daykw.douyin.task.VideoSearchThread;
import com.daykw.douyin.task.VideoUidThread;
import com.daykw.douyin.task.VideoZBSearchThread;
import com.daykw.douyin.utils.Const;

public class StartApp {
	private static Logger logger = Logger.getLogger(StartApp.class);

	public static void main(String[] args) {

		String node = null;
		if (args.length != 0 && args != null && StringUtils.isNotEmpty(args[0])) {
			node = args[0];
			Const.Node = node;
		}

		logger.info("当前节点node:" + node);
		SpringApplicationBuilder builder = new SpringApplicationBuilder(StartApp.class);

		builder.web(WebApplicationType.SERVLET).run();

		logger.info("##########spring boot 启动成功##########");

		try {

			PropertiesConfiguration props = new PropertiesConfiguration();
			props.setListDelimiter('#');

			InputStream stream = StartApp.class.getClassLoader().getResourceAsStream("application.properties");
			BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

			props.load(br);

			Const.SEARCH_AUTH = props.getBoolean("search.auth", false);
			Const.SEARCH_VIDEO = props.getBoolean("search.video", false);
			Const.SEARCH_ZB = props.getBoolean("search.zb", false);
			Const.DOWN_INFO = props.getBoolean("down.info", false);
			Const.DOWN_UID = props.getBoolean("down.uid", false);
			Const.DOWN_CMT = props.getBoolean("down.cmt", false);
			Const.DOWN_AUTH = props.getBoolean("down.auth", false);
			Const.DOWN_EXT = props.getBoolean("down.ext", false);
			Const.LOGIN = props.getBoolean("login", false);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (Const.LOGIN) {
			ScheduledExecutorService scheduled21 = Executors.newSingleThreadScheduledExecutor();
			scheduled21.scheduleAtFixedRate(new VideoLoginThread(), 0, 2, TimeUnit.MINUTES);
			logger.info("##########开启视频搜索###########");
		}

		if (Const.SEARCH_VIDEO) {
			ScheduledExecutorService scheduled1 = Executors.newSingleThreadScheduledExecutor();
			scheduled1.scheduleAtFixedRate(new VideoSearchThread(), 0, 3, TimeUnit.MINUTES);
			logger.info("##########开启视频搜索###########");
		}

		if (Const.SEARCH_AUTH) {
			ScheduledExecutorService scheduled6 = Executors.newSingleThreadScheduledExecutor();
			scheduled6.scheduleAtFixedRate(new VideoAuthSearchThread(), 0, 1, TimeUnit.MINUTES);
			logger.info("##########开启用户排行搜索###########");
		}

		if (Const.SEARCH_ZB) {

			ScheduledExecutorService scheduled9 = Executors.newSingleThreadScheduledExecutor();
			scheduled9.scheduleAtFixedRate(new VideoZBSearchThread(), 0, 1, TimeUnit.MINUTES);

			logger.info("##########开启用户主播搜索###########");
		}

		if (Const.DOWN_INFO) {

			ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(6);
			for (int i = 0; i < 1; i++) {
				scheduledThreadPool.scheduleAtFixedRate(new VideoInfoThread(), 0, 2, TimeUnit.MINUTES);
			}

			logger.info("##########开启视频详情下载###########");
		}
		if (Const.DOWN_EXT) {
			ScheduledExecutorService scheduled14 = Executors.newSingleThreadScheduledExecutor();
			scheduled14.scheduleAtFixedRate(new VideoExtThread(), 0, 1, TimeUnit.MINUTES);
		}

		if (Const.DOWN_UID) {
			ScheduledExecutorService scheduled3 = Executors.newSingleThreadScheduledExecutor();

			scheduled3.scheduleAtFixedRate(new VideoUidThread(), 0, 2, TimeUnit.MINUTES);

			logger.info("##########开启音乐详情和评论uid下载###########");
		}
		if (Const.DOWN_CMT) {
			ScheduledExecutorService scheduled4 = Executors.newSingleThreadScheduledExecutor();

			scheduled4.scheduleAtFixedRate(new VideoCmtThread(), 0, 30, TimeUnit.SECONDS);

			logger.info("##########开启音评论下载###########");

		}

		if (Const.DOWN_AUTH) {
			ScheduledExecutorService scheduled5 = Executors.newSingleThreadScheduledExecutor();
			scheduled5.scheduleAtFixedRate(new VideoAuthThread(), 0, 2, TimeUnit.MINUTES);
			ScheduledExecutorService scheduled11 = Executors.newSingleThreadScheduledExecutor();
			scheduled11.scheduleAtFixedRate(new VideoAuthVideoThread(), 0, 2, TimeUnit.MINUTES);

			logger.info("##########开启用户信息下载###########");
		}
	}

}
